package com.smart.web.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.smart.consts.AppInteractiveState;
import com.smart.consts.AppStatusData;
import com.smart.dao.Page;
import com.smart.dao.PageRequest;
import com.smart.model.Notice;
import com.smart.model.NoticeCond;
import com.smart.model.User;
import com.smart.model.UserViewRecord;
import com.smart.model.base.JsonModel;
import com.smart.util.ArrayUtil;
import com.smart.util.StringUtil;
/**
 * 公告模块处理器
 * @author zhuyong
 *
 */
@Controller
public class AppNoticeController extends BaseController{
	/***
	 * 根据类型获取公告
	 * @param request
	 * @param response
	 * @param type
	 */
	@RequestMapping("notice/getList")
	public void getList(String data,HttpServletRequest request, HttpServletResponse response) {
		JsonModel jsonModel = new JsonModel();
		AppStatusData statusObject = new AppStatusData();
		Page<Notice> page = null;
		int type = 1;
		//jsonModel.setHeaderValue("flag", "1");
		if (requestHandle(data, jsonModel,statusObject)) {
			String typeStr = jsonModel.getBodyValue("type");
			String pageNumber = jsonModel.getBodyValue("pageNumber");
			int intPageNumber = 1;
			if(StringUtil.isNotEmpty(pageNumber)) {
				try {
					intPageNumber = Integer.valueOf(pageNumber);
				} catch (Exception e) {
					statusObject=new AppStatusData("500", "请检查pageNumber参数");
					returnJson(response,statusObject, null);
				}
			}
			NoticeCond cond=new NoticeCond();
			PageRequest pageRequest = new PageRequest();
			if(StringUtil.isNotEmpty(typeStr)) {
				type = Integer.valueOf(typeStr);
			}
			cond.setType(type);
			cond.addOrderByDesc("timeCreated");
			pageRequest.setPageNumber(intPageNumber);
			pageRequest.setPageSize(100);
			page = noticeService.findAll(pageRequest,cond);
			statusObject = AppInteractiveState.SUCCESS_DATA;
			returnJson(response, statusObject, page);
		}
		returnJson(response, statusObject, page);
	}
	@RequestMapping("notice/viewNotice")
	public void viewNotice(String data,HttpServletRequest request, HttpServletResponse response) {
		AppStatusData statusObject = new AppStatusData();
		JsonModel jsonModel = new JsonModel();
		Notice notice = null;
		//jsonModel.setHeaderValue("flag", "1");
		try {
			if (requestHandle(data, jsonModel,statusObject)) {
				String strId = jsonModel.getBodyValue("id");
				String strUid = jsonModel.getBodyValue("uid");
				if(StringUtil.isNotEmpty(strId)) {
					notice = noticeService.findOne(Long.valueOf(strId));
//					user = userService.findOne(Long.valueOf(strUid));
					statusObject=AppInteractiveState.SUCCESS_DATA;
					if(notice != null) {
						notice.setViewCount(notice.getViewCount()+1);
						noticeService.save(notice);
						returnJson(response,statusObject,notice);
					}
				}else {
					statusObject=AppInteractiveState.ERROR_FORMAT;
					returnJson(response,statusObject,null);
				}
			}else {
				statusObject=AppInteractiveState.ERROR_FORMAT;
				returnJson(response,statusObject,null);
			}
			returnJson(response,statusObject,notice);
		} catch (Exception e) {
			statusObject = AppInteractiveState.EXCEPTION_DATA;
			returnJson(response,statusObject,null);
		}
	}
	public static void main(String[] args) throws UnsupportedEncodingException {
		String s=URLDecoder.decode("https://r.cnews.qq.com/getSubNewsChlidInterest?qqnetwork=wifi&omgbizid=b44496bbe7e0264ebe4a15fea100d9730d6f009011361f&appver=11.4.1_qnreading_4.8.40&apptype=ios&screen_height=736&logfrom=2&ssid=hongged&store=1&apptypeExt=qnreading&appversion=4.8.40&currentTab=kuaibao&muid=253036632475538723&activefrom=icon&screen_scale=3&screen_width=414&__qnr=21117b7b7244&isJailbreak=0&mac=020000000000&commonGray=1_3%7C13_2%7C2_1%7C38_4%7C22_2%7C14_0%7C17_0%7C30_0%7C12_1%7C19_1&idfv=829117D4-212E-43EB-A266-F3115BB10896&picSizeMode=0&omgid=3390024f99f9cb4a046b77165442e2c3b34f0010112804&device_model=iPhone8%2C2&startarticleid=&devid=829117D4-212E-43EB-A266-F3115BB10896&qimei=829117d4-212e-43eb-a266-f3115bb10896&idfa=B3A981FA-4A8F-4B64-8C58-47D16B8C003D", "utf-8");
		System.out.println(s);
	}
	@RequestMapping("notice/getQuick")
	public void getQuick(String data,HttpServletRequest request, HttpServletResponse response) {
		AppStatusData statusObject = new AppStatusData();
		JsonModel jsonModel = new JsonModel();
			if (requestHandle(data, jsonModel,statusObject)) {
				String notices = dictService.getDictValue("sys", "notice.quick");
				if(StringUtil.isEmpty(notices)) {
					notices = "胡先生成功下款5000元,张女士成功下款8000元,";
				}
				String [] ns =notices.split(",");
				List<String> list = ArrayUtil.asList(ns);
				returnJson(response,statusObject,list);
			}
		returnJson(response,statusObject,null);
	}
}
